<?php

namespace app\admin\controller;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Config;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="企业信息")
 */
class Company extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new \app\admin\model\Company();
        $this->connect_todatabase();
    }
 
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $admin = session("admin");
            if($admin["type"] == 1){
                $count = Db::connect('tmp')->table('company')
                    ->where($where)
                    ->count();
                $list = Db::connect('tmp')->table('company')
                    ->where($where)
                    ->page($page, $limit)
                    ->order($this->sort)
                    ->select();
            }else{
                $count = Db::connect('tmp')->table('company')
                    ->where($where) 
                    ->where("id",$admin["company_id"])
                    ->count();
                $list = Db::connect('tmp')->table('company')
                    ->where($where)
                    ->where("id",$admin["company_id"])
                    ->page($page, $limit)
                    ->order($this->sort)
                    ->select();
            }
            
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            if(empty($post["cur_type"])){
                $this->error("类型不能为空");
            }
            $data = [
                "company_name"=>$post["company_name"],
                "business_license_photo"=>$post["business_license_photo"],
                "company_logo"=>$post["company_logo"],
                "identity_card_front"=>$post["identity_card_front"],
                "identity_card_back"=>$post["identity_card_back"],
                "contact_person_avatar"=>$post["contact_person_avatar"],
                "contact_person_name"=>$post["contact_person_name"],
                "mobile"=>$post["mobile"],
                "cur_type"=>$post["cur_type"],
                "created_at"=>date("Y-m-d H:i:s",time())
            ];
            try {
                Db::connect('tmp')->table('company')->insert($data);
                $company_id = Db::connect('tmp')->table('company')->getLastInsID();
                $data_c = [
                    "username"=>$post["mobile"],
                    "password"=>password("123456"),
                    "type"=>2,
                    "company_id"=>$company_id
                ];
                Db::name("system_admin")->insert($data_c);
            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
         
            $this->success('保存成功');
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $admintype = session('admin.type');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            if(empty($post["cur_type"])){
                $this->error("类型不能为空");
            }
            $data = [
                "company_name"=>$post["company_name"],
                "business_license_photo"=>$post["business_license_photo"],
                "company_logo"=>$post["company_logo"],
                "identity_card_front"=>$post["identity_card_front"],
                "identity_card_back"=>$post["identity_card_back"],
                "contact_person_avatar"=>$post["contact_person_avatar"],
                "contact_person_name"=>$post["contact_person_name"],
                "mobile"=>$post["mobile"],
                "cur_type"=>$post["cur_type"],
                "updated_at"=>date("Y-m-d H:i:s",time())
            ]; 
            if($admintype == 1){
                $data["is_lock"] = $post["is_lock"];
            }
            try {
                Db::connect('tmp')->table('company')->where("id",$id)->update($data);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $this->success('保存成功');
        }else{
            $row = Db::connect('tmp')->table('company')->find($id);
            $this->assign('row', $row);
            $this->assign('admintype', $admintype);
            return $this->fetch();
        }   
        
    }

   
      /**
     * @NodeAnotation(title="删除")
     */
    public function delete($id)
    {
        $row = Db::connect('tmp')->table('company')->where('id', $id)->find();
        if(empty($row)){
            $this->error('数据不存在');
        }
        try {
            $save = Db::connect('tmp')->table('company')->where('id', $id)->update(["deleted_at"=>date("Y-m-d H:i:s",time())]);
        
        } catch (\Exception $e) {
            $this->error('删除失败');
        }
        $save ? $this->success('删除成功') : $this->error('删除失败');
    }
    
}